<?php

include_once '../AtacCommunicator/ACBusWaitingTimes.php';
include_once '../Model/BusStop.php';
include_once '../Utility/DebugLogs.php';
include_once '../Model/BusLinePath.php';
include_once '../Model/GPS.php';
include_once '../AtacCommunicator/ACBikesStatus.php';
include_once '../InfoManager/Constants.php';
include_once '../Utility/simplehtmldom/simple_html_dom.php';

ini_set('max_execution_time', '0'); // 0 = no limit.
ini_set("memory_limit",-1);

date_default_timezone_set('Europe/Rome');

$logfile = fopen($_SERVER['DOCUMENT_ROOT'].FILE_LOG,'a');
logthis("Stops_Unavailable");
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);

$mystops=array();
$out=array();


$stops = query("select IMPIANTO from `ifermata_fermateAutobus` where IMPIANTO not in (select fermata from ifermata_fl)");
$stops2 = query("select IMPIANTO from `ifermata_fermateAutobus` where IMPIANTO not in (select fermata from ifermata_pf)");

foreach($stops2 as $s){
	$stops[]=$s;
}

foreach($stops as $s){
	
$link = "http://muovi.roma.it/paline/palina/".$s['IMPIANTO'];
$page = getHTMLPageFromURL($link);
$html = str_get_html($page);

if(strpos($html,"La palina $s[IMPIANTO] non esiste")!==false){
	logthis($s['IMPIANTO']." disattivata");
	mysql_query("delete from ifermata_fermateAutobus where IMPIANTO='$s[IMPIANTO]'",$conn);
}
else
	logthis($s['IMPIANTO']." OK");

}

function getHTMLPageFromURL($url) {
	// create curl resource
	//echo "<br>Prima di curl_init:".date("l F d, Y, h:i:s"); 
	$AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1';
	$output = "";
	
	$ch = curl_init(); 
	
	// set url 
	curl_setopt($ch, CURLOPT_URL, $url); 
	curl_setopt($ch, CURLOPT_USERAGENT, $AGENT);
	//return the transfer as a string 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	// $output contains the output string 
	$output = curl_exec($ch); 
	
	// close curl resource to free up system resources 
	curl_close($ch);
	
	unset($ch);
	
	//$output = file_get_contents($url); 
	
	
	return $output;
}

function query($query)
{ 
	global $conn;
  $a = array();
  $ris = mysql_query($query,$conn);
  $mysqlerrno = mysql_errno($conn); 
  if ($mysqlerrno != 0)
  { 
    return false;
  }
  else if ($ris === true)
    return true;
  else
    while ($row = mysql_fetch_array($ris,MYSQL_ASSOC))
      $a[] = $row;
  return $a;  
}

?>
